import 'package:flutter/material.dart';
import 'package:flutter_jpush/flutter_jpush.dart' ;
import 'package:zhrs/mainpage/mainindex.dart';
import 'package:zhrs/utils/StaticContent.dart';
import 'pages/login.dart';
import 'package:camera/camera.dart';
import 'package:scoped_model/scoped_model.dart';
import 'model/app_model.dart';
import 'pages/index.dart';
import 'dart:convert' ;

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await initAsync();
  await _initJPush();

  runApp(MyApp());
}

List<CameraDescription> cameras;
initAsync() async{
  //获取相机信息
  cameras = await availableCameras();
}

void _initJPush() async
{
  /// 设置tags
  await FlutterJPush.startup();
  print("初始化jpush成功");
  // 获取 registrationID 有时可能会获取不到
  //var registrationID =await FlutterJPush.getRegistrationID();
  //print(registrationID);
  // 注册接收和打开 Notification()
  _initNotification();
}

void _initNotification() async {
  FlutterJPush.addConnectionChangeListener((bool connected) async {
      /// 是否连接，连接了才可以推送
      print("连接状态改变:$connected");
      if (connected) {
        FlutterJPush.getRegistrationID().then((String regId) async{
          print("主动获取设备号:$regId");
          StaticContent.RegistrationID=regId;
        });
      }

  });

  FlutterJPush.addReceiveNotificationListener(
          (JPushNotification notification) {
        print("收到推送提醒: "+notification.content);
      }
  );

  FlutterJPush.addReceiveOpenNotificationListener(
          (JPushNotification notification) async{
        print("打开了推送提醒: "+notification.content);
        StaticContent.information=notification.content;
        StaticContent.isonce=false;
        Map<String,dynamic> url = jsonDecode( notification.extras);
        if(url["index"]==null||url["url"]==null)
          return;
        StaticContent.index=url["index"];
        StaticContent.url=url["url"];
        if(StaticContent.contexts.widget is IndexPage)
        {
          print("进入了");
          StaticContent.isonce=true;
          StaticContent.pageState.Onchange(StaticContent.index, StaticContent.url);
          return;
        }
        if(StaticContent.contexts.widget is Mainindex)
        {
          StaticContent.isonce=true;
          StaticContent.pageState.Onchange(StaticContent.index, StaticContent.url);
          return;
        }

      /*  HttpManager.login(
            StaticContent.contexts, UserModel(loginname: "111111", password: "111111"),
            true
            );
       */
      }
  );
}
class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyApp();
  }
  // This widget is the root of your application.

}

class _MyApp extends State<MyApp>
{

  @override
  void initState() {
    // TODO: implement initState

    super.initState();
  }

  @override
  Widget build(BuildContext context)
  {
    final app = ScopedModel(

      model: AppModel(cameras: cameras),

      child: new MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "智慧人社",
       home:LoginPage( ) ,
       //home: Mainindex(index: 1,),
        //home:Medicare(),
        routes: {
          "/index":(context)=>IndexPage(),
        },
      ),
    );
    return app;
  }


}